package subject_set.lc200;

/**
 * @author haomin
 * @date 2023/04/24 20:23
 **/
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
public class Num572 {
    class Solution {
        public boolean isSubtree(TreeNode root, TreeNode subRoot) {
            if(subRoot == null) return true;
            if(root == null) return false;
            return isSubtreeHelper(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
        }
        public boolean isSubtreeHelper(TreeNode root, TreeNode subRoot) {
            if(root == null && subRoot == null) return true;
            if(root == null || subRoot == null) return false;
            if(root.val != subRoot.val) return false;
            return isSubtreeHelper(root.left, subRoot.left) && isSubtreeHelper(root.right, subRoot.right);
        }
    }
}